package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/app/product/cmd/api/internal/svc"
	"mall/app/product/cmd/api/internal/types"
	"mall/app/product/cmd/rpc/pb"
	"mall/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAttrGroupByCategoryIdByPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAttrGroupByCategoryIdByPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAttrGroupByCategoryIdByPageLogic {
	return &GetAttrGroupByCategoryIdByPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAttrGroupByCategoryIdByPageLogic) GetAttrGroupByCategoryIdByPage(req *types.GetAttrGroupListReq) (*pb.GetAttrGroupByCategoryIdResp, error) {
	resp, err := l.svcCtx.ProductRpc.GetAttrGroupByCategoryIdAndPage(l.ctx, &pb.GetAttrGroupByCategoryIdReq{
		Limit:      req.Limit,
		PageIndex:  req.Page,
		CategoryId: req.CategoryId,
		Key:        req.Key,
	})
	logx.Info(resp)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithMsg("获取属性分组失败"), "req: %+v err: %v", req, err)
	}
	return resp, nil
}
